package com.shuwang.goods.controller;

import com.shuwang.goods.domain.GoodsSkuRequest;
import com.shuwang.goods.exception.ConflictException;
import com.shuwang.goods.exception.ForbiddenException;
import com.shuwang.goods.exception.NotFoundException;
import com.shuwang.goods.mapper.GoodsInfoMapper;
import com.shuwang.goods.mapper.GoodsSkuMapper;
import com.shuwang.goods.model.GoodsInfoExample;
import com.shuwang.goods.model.GoodsSku;
import com.shuwang.goods.model.GoodsSkuExample;
import com.shuwang.goods.until.IDBuilder;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2017/6/5.
 */
@RestController
@RequestMapping(value = "goods")
@Api(value = "goods_sku",description = "新增产品sku信息")
public class GoodsSkuController {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private GoodsSkuMapper goodsSkuMapper;
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @RequestMapping(value = "sku", method = RequestMethod.POST)
    @ApiOperation(value = "产品属性信息接口")
    public GoodsSku createGoodsSku(@ApiParam(value = "产品属性信息实体类") @Valid @RequestBody GoodsSku model) {

        GoodsInfoExample goodsInfoExample = new GoodsInfoExample();
        goodsInfoExample.createCriteria().andGoodsIdEqualTo(model.getGoodsId());
        if (goodsInfoMapper.selectByExample(goodsInfoExample).isEmpty()) {
            log.debug("该商品Id不存在，请重新确认");
            throw new ConflictException("该商品Id不存在，请重新确认");
        } else {

            GoodsSkuExample goodsSkuExample = new GoodsSkuExample();
            goodsSkuExample.createCriteria().andGoodsIdEqualTo(model.getGoodsId());
            if (!goodsSkuMapper.selectByExample(goodsSkuExample).isEmpty()) {
                log.debug("该商品属性信息已经存在");
                throw new ConflictException("该商品属性信息已经存在");
            }

            GoodsSku goodsSku = new GoodsSku();
            Long goodsId = IDBuilder.CreateAnyId("110");
            goodsSku.setGoodsId(goodsId);
            BeanUtils.copyProperties(model, goodsSku);
            goodsSkuMapper.insertSelective(goodsSku);

            GoodsSku success = goodsSkuMapper.selectByPrimaryKey(goodsSku.getGoodsId());
            if (success == null) {
                log.debug("产品属性信息入库失败");
                throw new NotFoundException("产品属性信息入库失败");
            }
            return success;
        }
    }

    @RequestMapping(value = "sku/{goodsId}",method = {RequestMethod.POST,RequestMethod.PUT})
    @ApiOperation(value = "根据goodsId修改产品属性信息",response = GoodsSku.class)
    public GoodsSku reviseGoodsSkuController(
            @ApiParam(value = "请输入goodsId") @PathVariable Long goodsId,
            @ApiParam(value = "请输入产品属性信息", required = true) @RequestBody @Valid GoodsSkuRequest request) {

        GoodsSku goodsSku = new GoodsSku();
        if( goodsSkuMapper.selectByPrimaryKey(goodsId) == null ){
            log.debug("该产品属性信息不存在");
            throw new NotFoundException("该产品属性信息不存在");
        }

        BeanUtils.copyProperties(request,goodsSku);
        goodsSku.setGoodsId(goodsId);

        goodsSkuMapper.updateByPrimaryKey(goodsSku);
        if(goodsSku==null){
            log.debug("商品属性信息入库失败");
            throw new NotFoundException("商品属性信息入库失败");
        }
        return goodsSku ;
    }


    @RequestMapping(value = "sku/getgoodsId/{goodsId}",method = {RequestMethod.GET})
    @ApiOperation(value = "根据goodsId查询产品属性信息", httpMethod = "", response = GoodsSku.class)
    public List<GoodsSku> getGoodsSkuByGoodsIdController(@ApiParam(value = "请输入goodsId") @PathVariable Long goodsId ) {

        GoodsSkuExample goodsSkuExample =new GoodsSkuExample();
        goodsSkuExample.createCriteria().andGoodsIdEqualTo(goodsId);
        List<GoodsSku> goodsSku =goodsSkuMapper.selectByExample(goodsSkuExample);
        if( goodsSku == null ){
            log.debug("该产品属性联信息不存在");
            throw new NotFoundException("该产品属性联信息不存在");
        }
        return goodsSku;
    }

    @RequestMapping(value = "sku/{goodsId}",method = {RequestMethod.DELETE})
    @ApiOperation(value = "根据goodsId删除产品属性信息", httpMethod = "DELETE", response = GoodsSku.class)
    public List<GoodsSku> deleteGoodsSkuByGoodsIdController(@ApiParam(value = "请输入goodsId") @PathVariable Long goodsId ) {

        GoodsSkuExample goodsSkuExample = new GoodsSkuExample();
        goodsSkuExample.createCriteria().andGoodsIdEqualTo(goodsId);
        List<GoodsSku> goodsSku =goodsSkuMapper.selectByExample(goodsSkuExample);
        if (goodsSku.isEmpty()) {
            log.debug("该产品属性信息不存在");
            throw new NotFoundException("该产品属性信息不存在");
        }
        goodsSkuMapper.deleteByExample(goodsSkuExample);

        if (!goodsSkuMapper.selectByExample(goodsSkuExample).isEmpty()) {
            log.debug("该产品属性信息删除失败");
            throw new ForbiddenException("该产品属性信息删除失败");

        }
        return goodsSku;
    }
}
